<?php
/**
 * fb.class
 *
 * meta class for include and configure firephp/chromephp
 *
 */

function __browserconsole_init() {

	if( CONSOLE )
		return __browserconsole_empty();

	$bcc = config::instance()->browserconsole;

	if( strtolower( $bcc['enabled'] ) === 'dev' )
		$bcc['enabled'] = request::instance()->is_developer();
	
	$bcc['enabled'] = (!!$bcc['enabled'] || stripos(',on,yes,true',$bcc['enabled']));


	if( !$bcc['enabled'] )
		return __browserconsole_empty();

	if( request::instance()->is_firefox() ) {

		require_once( dirname(__FILE__).'/BrowserConsole/FirePHPCore/fb.php' );
		class bc extends fb {
			static function enabled() {
				return true;//var_dump(debug_backtrace(false)); die;
			}
		};

		bc::setEnabled( true );
		bc::setOptions( $bcc );

	} elseif( request::instance()->is_chrome() ) {

		if(!isset($bcc['backtrace_level']))
			$bcc['backtrace_level'] = 2;

		require_once( dirname(__FILE__).'/BrowserConsole/ChromePhp.php' );
		class bc extends ChromePHP {
			static function enabled() {
				$log_inside_classes = array_filter( array_map('trim',explode(',',@config::instance()->browserconsole['loginsideclasses'])) );
				if (empty($log_inside_classes)) return true;
				$trace = debug_backtrace(false); unset($trace[0]); $classes = array(); foreach ($trace as $c) $classes[] = @$c['class']; $classes = array_unique($classes);
				if (sizeof(array_intersect($classes, $log_inside_classes))) return true;
				return false;
				var_dump($classes, $log_inside_classes, array_intersect($classes, $log_inside_classes)); die;
			}

			public static function log( ) {
				return parent::_log(array_reverse(func_get_args()) + array('type' => self::LOG));
			}
//			public static function info( $a, $b = null, $c = null ) { return self::log( $a, $b, $c ); }
//			public static function group( ) { }
//			public static function group_end( ) { }
		};

		$bc = bc::getInstance();
		foreach ($bcc as $k => $v)
			$bc->addSetting( $k, $v );

		$f = (!empty($bcc['file']) ? $bcc['file'] : 'browserconsole');
		bc::useFile( SITEPATH . 'files/' . $f, '/files/' . $f );

	} else
		return __browserconsole_empty();
}

function __browserconsole_empty( ) {
	class bc { static public function __callStatic($a,$b){} static function enabled(){} static public function log(){} static public function info(){} static public function group(){} static public function group_end(){} static public function error(){} };
}

__browserconsole_init( );
